package com.five.community.controller;

import com.five.community.bean.Person;
import com.five.community.bean.Pet;
import com.five.community.bean.vo.PetVo;
import com.five.community.service.PersonService;
import com.five.community.service.PetService;
import com.five.community.util.FileUtils;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/pet")
public class PetController {
    @Autowired
    private PetService petService;
    @Autowired
    private PersonService personService;

    @RequestMapping("/listUi")
    public String listUi(@RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum,
                         @RequestParam(value = "pageSize",required = false,defaultValue = "5")Integer pageSize,
                         Model model, ModelMap map){
        try {

            List<PetVo> pages= petService.selectAllPets(pageNum,pageSize);
            List<PetVo> pets = petService.selectAllPet();
            PageInfo<PetVo> pageInfo = new PageInfo<>(pages);
            model.addAttribute("pageInfo",pageInfo);
            map.addAttribute("pets",pets);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "pet";
    }
    //添加画面
    @RequestMapping("/addUi")
    public String addUi(ModelMap map) {
        try {
            List<Person> persons = personService.selectAll();
            map.addAttribute("persons", persons);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "pet_add";
    }
    //添加方法
    @RequestMapping("/add")
    public String add(PetVo petVo, MultipartFile photos) {
        long id1 = Long.parseLong(petVo.getPname());
        String fileName = "";
        try {
            fileName = FileUtils.fileUpload(photos);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 设置数据
        petVo.setPicture(fileName);
        petVo.setCreateTime(new Date());
        try {
            Pet pet = new Pet(fileName, id1, petVo.getName(), petVo.getColor(),
                    petVo.getRemark(), petVo.getAdoptTime(), new Date());
            petService.insert(pet);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "redirect:/pet/addUi";
    }

    // 删除方法
    @RequestMapping("/delete")
    public String delete(long id) {
        try {
            petService.deleteById(id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "redirect:/pet/listUi";
    }
    //查询方法
    @RequestMapping("/select")
    public String select(@RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum,
                         @RequestParam(value = "pageSize",required = false,defaultValue = "5")Integer pageSize,
                         String name, ModelMap map,Model model) {
        if (name == "") {
            return "redirect:/pet/listUi";
        } else {
            try {
                List<PetVo> pages= petService.selectAllByName(pageNum,pageSize,name);
                List<PetVo> pets = petService.selectByName(name);
                PageInfo<PetVo> pageInfo = new PageInfo<>(pages);
                model.addAttribute("pageInfo",pageInfo);
                map.addAttribute("pets", pets);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            return "pet";
        }

    }
    //更新画面
    @RequestMapping("/editUi")
    public String editUi(long id, ModelMap map1){
        try {
            Pet pet = petService.selectById(id);
            List<Person> person = personService.selectAll();
            map1.addAttribute("person",person);
            map1.addAttribute("pet",pet);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "pet_edit";
    }
    //更新方法
    @RequestMapping("/edit")
    public String edit(PetVo petVo, MultipartFile photos) {
        long id1 = Long.parseLong(petVo.getPname());
        String fileName = "";
        try {
            fileName = FileUtils.fileUpload(photos);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 设置数据
        petVo.setPicture(fileName);
        petVo.setCreateTime(new Date());
        try {
            Pet pet = new Pet(petVo.getId(),fileName, id1, petVo.getName(), petVo.getColor(),
                    petVo.getRemark(), petVo.getAdoptTime(), new Date());
            petService.update(pet);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "pet_edit";
    }
}
